package jucPC;

import jdk.nashorn.internal.ir.CallNode;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * A 执行完调用B，B执行完调用C,C执行完调用A
 */
public class C {
    public static void main(String[] args) {
        Data3 data = new Data3();

        new Thread(()->{
            for (int i = 0; i < 10; i++) {
                try {
                    data.printA();
                } catch (Exception e) {
                    e.printStackTrace();
                }

            }
        },"A").start();
        new Thread(()->{
            for (int i = 0; i < 10; i++) {
                try {
                    data.printB();
                } catch (Exception e) {
                    e.printStackTrace();
                }

            }
        },"B").start();
        new Thread(()->{
            for (int i = 0; i < 10; i++) {
                try {
                    data.printC();
                } catch (Exception e) {
                    e.printStackTrace();
                }

            }
        },"C").start();


    }
}
//判断等待，业务，通知
class Data3 {//数字，资源类
    private int num = 1;
//只要是并发编程一定会有锁。

    Lock lock = new ReentrantLock();
    Condition condition1 = lock.newCondition();
    Condition condition2 = lock.newCondition();
    Condition condition3 = lock.newCondition();


    public void printA(){
        lock.lock();
        try {
//            业务代码,判断=》执行=》通知
            while (num != 1) {
//                等待
                condition1.await();
            }
            System.out.println(Thread.currentThread().getName()+"=》AAAAA ");
//            唤醒，唤醒指定的人，B
            num=2;
            condition2.signal();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }


    }
    public void printB(){
        lock.lock();
        try {
//            业务代码,判断=》执行=》通知
            while (num != 2) {
//                等待
                condition2.await();
            }
            System.out.println(Thread.currentThread().getName()+"=》BBBBB");
//            唤醒，唤醒指定的人，B
            num=3;
            condition3.signal();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }
    }
    public void printC(){
        lock.lock();
        try {
//            业务代码,判断=》执行=》通知
            while (num != 3) {
//                等待
                condition3.await();
            }
            System.out.println(Thread.currentThread().getName()+"=》CCCCC");
//            唤醒，唤醒指定的人，B
            num=1;
            condition1.signal();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }

    }


}
/*
执行结果：
A=》AAAAA
B=》BBBBB
C=》CCCCC
A=》AAAAA
B=》BBBBB
C=》CCCCC
A=》AAAAA
B=》BBBBB
C=》CCCCC
A=》AAAAA
B=》BBBBB
C=》CCCCC
A=》AAAAA
B=》BBBBB
C=》CCCCC
A=》AAAAA
B=》BBBBB
C=》CCCCC
A=》AAAAA
B=》BBBBB
C=》CCCCC
A=》AAAAA
B=》BBBBB
C=》CCCCC
A=》AAAAA
B=》BBBBB
C=》CCCCC
A=》AAAAA
B=》BBBBB
C=》CCCCC
 */